﻿Ext.define('Html5App.controller.ProgressController', {
	extend : 'Ext.app.Controller',
	config : {
		models : [],
		views : [],
		refs : {
			progressHome : 'progressHome'
		},
		control : {
			progressHome : {
				activate : function(newActiveItem, t, oldActiveItem, eOpts) {
					util.showLoadMask(null);
					var salesProgressList,salesProgressDetailList;
					config.database.transaction(
				        function (tx) {
				        	tx.executeSql(
				        		"select tmp.BrandCode, sum(tmp.Target1 * 1000) as Target, sum(tmp.Actual) as Actual, " +
							  	" CASE when sum(tmp.Target1*1000)<=0 then 100 else sum(tmp.Actual)/sum(tmp.Target1*1000)*100 end as exponential," +
								" CASE when sum(tmp.Target1*1000)<=sum(tmp.Actual) then 0 else sum(tmp.Target1*1000)-sum(tmp.Actual) end as owe," +
							  	" sum(tmp.CustomerNum) as Custs " +
								" from ( " +
								 " select a.BrandCode, sum(a.Target) as Target1, 0 as Actual, 0 as CustomerNum " +
								 " from SalesBrandTarget a " +
								 " where a.SalesCode='"+config.current_user.user_name+"' " +
								 " group by a.BrandCode " +
								 " union all " +
								 " select a.CNTLACCT as BrandCode, 0 as Target1, sum(a.OrderAmt) as Actual, 0 as CustomerNum  " +
								 " from TAllSalesDataPK a, Customer b " +
								 " where a.Customer = b.CustomerCode and a.SRCode = b.SalesCode " +
								 " and a.ORDDATE >= '"+custom.getCurrentMonthFirst()+"' " +
								 " And a.ORDDATE <= '"+custom.getCurrentMonthLast()+"' " +
								 " and a.SRCode='"+config.current_user.user_name+"'  " +
								 " and b.SalesCode='"+config.current_user.user_name+"' " +
								 " group by a.CNTLACCT " +
								 " union all " +
								 " select custtmp.BrandCode, 0 as Target1, 0 as Actual, Count(custtmp.Customer) as CustomerNum " +
								 " from ( " +
								  " select a.CNTLACCT as BrandCode, a.Customer  " +
								  " from TAllSalesDataPK a, Customer b " +
								  " where a.Customer = b.CustomerCode and a.SRCode = b.SalesCode " +
								  " and a.ORDDATE >= '"+custom.getCurrentMonthFirst()+"' " +
								  " And a.ORDDATE <= '"+custom.getCurrentMonthLast()+"' " +
								  " and a.SRCode='"+config.current_user.user_name+"' " +
								  " and b.SalesCode='"+config.current_user.user_name+"' " +
								  " group by a.CNTLACCT, a.Customer " +
								 " ) custtmp group by custtmp.BrandCode " +
								" ) tmp, " +
								" (select b.BrandName " +
								" from Item a left join Brand b on a.BrandID = b.BrandCode " +
								// " where a.SalesCode='"+config.current_user.user_name+"' and b.SalesCode ='"+config.current_user.user_name+"' " +
								" group by b.BrandName " +
								" ) tmp1 " +
								" where tmp.BrandCode = tmp1.BrandName " +
								" group by tmp.BrandCode " +
								" order by tmp.BrandCode", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				salesProgressList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        	tx.executeSql(
				        		"select tmp.BrandCode, tmp.Item, tmp.UNIT, tmp.Description,Round((sum(tmp.Amt)/sum(tmp.Qty)),2) as unitPrice, sum(tmp.Qty) as SalesQty, sum(tmp.Amt) as Amount " +
								" from ( " +
								 " select a.CNTLACCT as BrandCode, a.Item, max(a.ORDUNIT) as UNIT, Max(a.[DESC]) as Description, sum(a.OCTN + a.OPC) as Qty, sum(a.OrderAmt) as Amt " +
								 " from TAllSalesDataPK a, Customer b " +
								 " where a.Customer = b.CustomerCode and a.SRCode = b.SalesCode " +
								 " and a.ORDDATE >= '"+custom.getCurrentMonthFirst()+"' " +
								 " and a.ORDDATE <= '"+custom.getCurrentMonthLast()+"' " +
								 " and a.SRCode = '"+config.current_user.user_name+"' " +
								 " and b.SalesCode = '"+config.current_user.user_name+"' " +
								 " group by a.CNTLACCT, a.Item) tmp " +
								" group by tmp.BrandCode, tmp.Item, tmp.UNIT, tmp.Description " +
								" order by tmp.BrandCode, tmp.Item", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				salesProgressDetailList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				            tx.executeSql("select AreaCode, max(AreaName) as AreaName2 " +
			                		",sum(CASE WHEN Orders>0 THEN 1 ELSE 0 END) as m1"+
			                		",sum(1) as m2"+
			                		",sum(CASE WHEN Other3>0 THEN 1 ELSE 0 END) as mp"+
			                		",sum(CASE WHEN IsGolden45>0 AND Golden45s>0 THEN 1 ELSE 0 END) as n1"+
			                		",sum(CASE WHEN IsGolden45>0 THEN 1 ELSE 0 END) as n2"+
					        		" from Golden45Sales where SalesCode='"+config.current_user.user_name+"' group by AreaCode order by AreaCode",
			                    [],
			                    function (tx, result) {
			                    	htmlStr = '';
			                    	Ext.getCmp("this-month-introduction-progress").setHtml(htmlStr);
			                    	htmlStr += '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">';
			                    	config.area_code_arr = [];
			                    	var n1Total=0,n2Total=0,mpTotal=0,m1Total=0,m2Total=0;
			                    	for (var i = 0; i < result.rows.length; i++) {
			                    		n1Total=Number(Number(n1Total)+Number(result.rows.item(i)['n1']));
			                    		n2Total=Number(Number(n2Total)+Number(result.rows.item(i)['n2']));
			                    		mpTotal=Number(Number(mpTotal)+Number(result.rows.item(i)['mp']));
			                    		m1Total=Number(Number(m1Total)+Number(result.rows.item(i)['m1']));
			                    		m2Total=Number(Number(m2Total)+Number(result.rows.item(i)['m2']));
			                    		config.area_code_arr.push(result.rows.item(i)['AreaCode']);
			                    		if(i%2==0){
			                    			htmlStr += '<tr style="height:2rem;background-color:#F5F5F5;" onclick="javascript:custom.thisMonthIntroductionProgressToView(\''+result.rows.item(i)['AreaCode']+'\');">';
			                    		}else{
			                    			htmlStr += '<tr style="height:2rem;background-color:#FFFFFF;" onclick="javascript:custom.thisMonthIntroductionProgressToView(\''+result.rows.item(i)['AreaCode']+'\');">';
			                    		}
										    htmlStr +=  '<td style="border-right:1px solid #CFCFCF;width:20%;padding-left:0.2rem;">'+result.rows.item(i)['AreaName2']+'</td>'+
													    '<td style="border-right:1px solid #CFCFCF;width:16%;padding-left:0.5rem;">'+result.rows.item(i)['n1']+'/'+result.rows.item(i)['n2']+'</td>'+
													    '<td style="border-right:1px solid #CFCFCF;width:16%;padding-left:0.5rem;">'+result.rows.item(i)['mp']+'/'+result.rows.item(i)['m2']+'</td>'+
													    '<td style="width:48%;padding-left:0.2rem;">'+result.rows.item(i)['m1']+'/'+result.rows.item(i)['m2']+'個戶口</td>'+
													  '</tr>'+
													  '<tr style="display:none;" id="'+result.rows.item(i)['AreaCode']+'_tr">'+
								  						'<td colspan="4" id="'+result.rows.item(i)['AreaCode']+'_td">'+
								  						'</td>'+
											  		  '</tr>';
			                    	}
			                    	htmlStr += '</table>';
			                    	Ext.getCmp("this-month-introduction-progress").setHtml(htmlStr);
			                    	htmlStr = '';
			                    	htmlStr += '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">';
			                    	htmlStr += '<tr style="height:2rem;background-color:#FFFFFF;font-weight:bold;">'+
				                   				 '<td style="border-right:1px solid #CFCFCF;width:20%;padding-left:0.2rem;">總戶口</td>'+
												     '<td style="border-right:1px solid #CFCFCF;width:16%;padding-left:0.5rem;">'+n1Total+'/'+n2Total+'</td>'+
												     '<td style="border-right:1px solid #CFCFCF;width:16%;padding-left:0.5rem;">'+mpTotal+'/'+m2Total+'</td>'+
												     '<td style="width:48%;padding-left:0.2rem;">'+m1Total+'/'+m2Total+'個戶口</td>'+
												  '</tr>';
				                   	htmlStr += '</table>';
			                    	Ext.getCmp("this-month-introduction-progress-sum").setHtml(htmlStr);
			                    },
			                    function (tx, error) {
			                    	
			                    }
			                );
				        }
				    );
					setTimeout(function dataShow(){
						var htmlStr = '';
                        Ext.getCmp("this-month-sales-progress").setHtml(htmlStr);
						if(salesProgressList && Number(salesProgressList.length)>0){
							htmlStr += '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">';
                    		config.brand_code_arr = [];
                    		var targetTotal=0,actualTotal=0,exponentialTotal=0,oweTotal=0;
							for(var i=0;i<salesProgressList.length;i++){
					  			var progress = salesProgressList.item(i);
                    			targetTotal=Number(Number(targetTotal)+Number(progress["Target"]));
                    			actualTotal=Number(Number(actualTotal)+Number(progress["Actual"]));
                    			config.brand_code_arr.push(progress["BrandCode"]);
                    			if(i%2==0){
                    				 htmlStr +=   '<tr style="height:2rem;background-color:#F5F5F5;" onclick="javascript:custom.thisMonthSalesProgressToView(\''+progress["BrandCode"]+'\');">';
                    			}else{
                    				 htmlStr +=   '<tr style="height:2rem;background-color:#FFFFFF;" onclick="javascript:custom.thisMonthSalesProgressToView(\''+progress["BrandCode"]+'\');">';
                    			}
                    			htmlStr +=   		''+
												    '<td style="border-right:1px solid #CFCFCF;text-align:center;width:10%;"><img style="width:1.5rem;height:auto;" src="'+cordova.file.dataDirectory +'/Logo/Brand/'+progress["BrandCode"]+'.JPG"/></td>'+
												    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-left:0.1rem;">'+progress["BrandCode"]+'</td>'+
												    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(progress["Target"],0)+'</td>'+
												    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(progress["Actual"],0)+'</td>'+
												    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-right:0.2rem;text-align:right;">'+progress["exponential"]+'</td>'+
												    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-right:0.2rem;text-align:right;">'+progress["owe"]+'</td>'+
												    '<td style="width:15%;padding-right:0.2rem;text-align:right;">'+progress["Custs"]+'</td>'+
												  '</tr>'+
												  '<tr style="display:none;" id="'+progress["BrandCode"]+'_Sales_Progress_Detail">'+
												  	'<td colspan="7">'+
												        '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="color:#4D4D4D;">'+
												          '<tr style="height:1.6rem;background-color:#E1E1E1;font-weight:bold;text-align:center;">'+
												            '<td style="border-right:1px solid #CFCFCF;width:40%;">貨品</td>'+
												            '<td style="border-right:1px solid #CFCFCF;width:15%;">單位</td>'+
												            '<td style="border-right:1px solid #CFCFCF;width:15%;">單價</td>'+
												            '<td style="border-right:1px solid #CFCFCF;width:15%;">數量</td>'+
												            '<td style="border-right:1px solid #CFCFCF;width:15%;">總額</td>'+
												          '</tr>';
								if(salesProgressDetailList && Number(salesProgressDetailList.length)>0){
									for(var j=0;j<salesProgressDetailList.length;j++){
	                    				var progressDetail = salesProgressDetailList.item(j);
	                    				if(progress["BrandCode"] == progressDetail["BrandCode"]){
	                    					htmlStr += '<tr style="height:1.6rem;background-color:#E1E1E1;">'+
											            '<td style="border-right:1px solid #CFCFCF;padding-left:0.2rem;">'+progressDetail["Description"]+'</td>'+
											            '<td style="border-right:1px solid #CFCFCF;padding-left:0.2rem;text-align:center;">'+progressDetail["UNIT"]+'</td>'+
											            '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(progressDetail["unitPrice"],2)+'</td>'+
											            '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(progressDetail["SalesQty"],0)+'</td>'+
											            '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(progressDetail["Amount"],0)+'</td>'+
											          '</tr>';
	                    				}
	                    			}
								}
								htmlStr +=   '</table>'+
											'</td>'+
										  '</tr>';
							}
							if(Number(targetTotal)<=0){
                    			exponentialTotal = 100;
                    		}else{
                    			exponentialTotal = Number(Number(actualTotal/targetTotal)*100).toFixed(2);
                    		}
                    		oweTotal = Number(targetTotal) - Number(actualTotal);
                    		if(Number(oweTotal)<=0){
                    			oweTotal = 0;
                    		}
                    		htmlStr +=   '</table>';
                			Ext.getCmp("this-month-sales-progress").setHtml(htmlStr);
                			htmlStr = '';
                			htmlStr += '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">';
                			htmlStr +=   '<tr style="height:2rem;background-color:#FFFFFF;font-weight:bold;">'+
										    '<td style="border-right:1px solid #CFCFCF;text-align:center;width:10%;">&nbsp;</td>'+
										    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-left:0.1rem;">總計</td>'+
										    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-left:0.1rem;">'+custom.thousandBitSeparator(targetTotal,0)+'</td>'+
										    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-left:0.1rem;">'+custom.thousandBitSeparator(actualTotal,0)+'</td>'+
										    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-left:0.1rem;">'+exponentialTotal+'</td>'+
										    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-left:0.1rem;">'+oweTotal+'</td>'+
										    '<td style="width:15%;padding-left:0.1rem;">&nbsp;</td>'+
										  '</tr>';
                			htmlStr +=   '</table>';
                			Ext.getCmp("this-month-sales-progress-sum").setHtml(htmlStr);
						}
						util.hideLoadMask();
					},3000);
				}
			}
		}
	}
});